<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 执行明细管理
 * @package app\recorddetail
 * @author LJQ<245369209@qq.com>
 */
class Recorddetail extends Api

{

    /**
     *  执行明细列表
     * @param string
     * @return Json
     */

    public function recorddetailList($recordId=null,$recorddetailId=null){

        if (empty($recordId)) {
            return json(['code'=>0,'msg'=>'没有找到预案']);
        }

        $where['rescueExecId']=['eq',$recordId];
        if ($recorddetailId) {
            $where['id']=['eq',$recorddetailId];
        }

        //分页参数
        if(($limit = input('limit')) && ($page = input('page'))){
            $startLimit =  ($page-1)*$limit;
            // 执行明细列表
            $info =collection(model('recorddetail')->where($where)->limit($startLimit.','.$limit)->order('stepIndex')->select())->toArray();
        }else{
            //执行明细列表
            $info =collection(model('recorddetail')->where($where)->order('stepIndex')->select())->toArray();
        }

        //总数
        $count = model('recorddetail')->where($where)->count('id');

        if ($info && $count == 0) {
            return json(['code'=>0,'msg'=>'没有找到执行明细']);
        }

        if ($count > 0){
            return json(['code'=>1,'msg'=>'执行明细','count'=>$count,'data'=>$info]);
        }
        return json(['code'=>0,'msg'=>'数据异常']);
    }


    //添加执行明细
    public function add($data){

        if (empty($data)) {
            return json(['code'=>0,'msg'=>'数据异常,请检查']);
        }
        $model = model('recorddetail');
        $result = $model->change($data);
        if (false != $result) {
            setlog('recorddetailadd','recorddetail',session('user_auth.id'));
            return json(['code'=>1,'msg'=>'执行明细添加成功']);
        } else {
            return json(['code'=>0,'msg'=>'执行明细添加失败,请检查']);
        }
    }



    /**
     * 修改执行明细
     * @param  string recorddetailId 执行明细id,
     * @return Json
     */
    public function edit($data,$recorddetailId){

        if (empty($recorddetailId)) {
            return json(['code'=>0,'msg'=>'执行明细不存在,请检查']);
        }
        $model = model('recorddetail');
        if ($model->change($data)) {
            setlog('recorddetailupdate','recorddetail',session('user_auth.id'));
            return json(['code'=>1,'msg'=>'执行明细修改成功']);
        }else{
            return json(['code'=>0,'msg'=>'执行明细修改失败,请检查']);
        }
    }
    /**
     *  删除执行明细
     * @param string recorddetailId 执行明细id,
     * @param Json
     */

    public function del($recorddetailId) {
        $recorddetailId = input('recorddetailId',0,'intval');

        $model = model('recorddetail');
        if (empty ( $recorddetailId )||empty(($model->where(array('id'=>$recorddetailId))->find()))) {
            return  json(['code'=>0,'msg'=>'执行明细id不存在,请检查']);
        }
        if($model->destroy(['id'=>$recorddetailId]) !== FALSE){
            setlog('recorddetaildel','recorddetail',session('user_auth.id'));
            return json(['code'=>1,'msg'=>'执行明细删除成功']);
        }else{
            return $this->json(['code'=>0,'msg'=>'执行明细删除失败,请检查']);
        }
    }


    /**
     * 申请审核
     * @param string $hazardId
     */
    public function audit($planId = null){
        $wfType = 1;
        $workflow = model('Workflow')->where('wfType',$wfType)->find();
        if(empty($workflow)||empty($workflow->steps)){
            return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
        }else{
            $plan = model('Plan')->get($planId);
            if($plan->isUpdate(true)->save(['checkStatus'=>2]) !== false){
                $data['title'] = $plan['title'];
                $data['wfFlowInfoId'] = $workflow['id'];
                $data['wfType'] = $wfType;
                $data['busiId'] = $planId;
                $data['applyPerson'] = session('user_auth.id');
                if(model('Wfitem')->save($data)){
                    return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
                }else{
                    return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
                }
            }
        }
        return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);

    }

}
